package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.ExtCproduct;
import cn.learn.domain.cargo.ExtCproductExample;
import cn.learn.domain.cargo.Factory;
import cn.learn.domain.cargo.FactoryExample;
import cn.learn.service.cargo.ExtCproductService;
import cn.learn.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    /**
     * 进入附件展示界面
     * @return
     */
    @RequestMapping(value = "/list", name = "展示附件列表")
    public String list(String contractId, String contractProductId, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        // 已有附件展示
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractIdEqualTo(contractId); // 合同ID
        criteria.andContractProductIdEqualTo(contractProductId); // 货物ID
        PageInfo resultData = extCproductService.findAll(extCproductExample, pageNum, pageSize);
        request.setAttribute("pageList", resultData);
        // 添加附件时列表展示
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> resultFactory = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", resultFactory);
        request.setAttribute("contractProductId", contractProductId);
        request.setAttribute("contractId", contractId);
        return "/cargo/extc/extc-list";
    }

    /**
     * 添加一条
     * @param extCproduct
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加一条")
    public String addOne(ExtCproduct extCproduct) {
        extCproduct.setCompanyId(companyId);
        extCproduct.setCompanyName(companyName);
        extCproductService.save(extCproduct);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + extCproduct.getContractId() + "&contractProductId=" + extCproduct.getContractProductId();
    }

    /**
     * 跳转到修改页面 与 需改内容
     * String id, String contractId, String contractProductId
     * @return
     */
    @RequestMapping(value = "updateOne", name = "修改附件内容")
    public String updateOne(ExtCproduct extCproduct) {
        if (request.getMethod().equals("POST")) {
            extCproduct.setCompanyId(companyId);
            extCproduct.setCompanyName(companyName);
            extCproductService.update(extCproduct);
            return "redirect:/cargo/extCproduct/list.do?contractId=" + extCproduct.getContractId() + "&contractProductId=" + extCproduct.getContractProductId();
        }
        // 源数据内容
        ExtCproduct extData = extCproductService.findById(extCproduct.getId());
        // 查找选择框内容
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> resultData = factoryService.findAll(factoryExample);
        // 封装数据
        request.setAttribute("data", extData);
        request.setAttribute("factoryList", resultData);
        request.setAttribute("contractProductId", extCproduct.getContractProductId());
        request.setAttribute("contractId", extCproduct.getContractId());
        return "/cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete", name = "删除附件")
    public String delete(String id, String contractId, String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId + "&contractProductId=" + contractProductId;
    }

}
